import java.util.Iterator;
import java.io.*;
import java.util.EmptyStackException;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader in= null;
		PrintWriter out=null;
		try{
			if (args.length==0) {
				out=new PrintWriter(new FileWriter("out.txt"));
				in=new BufferedReader(new FileReader("in.txt"));
				}else if(args.length<2){
					System.out.println("File name error!");
				}
				else{
					in=new BufferedReader(new FileReader(args[0]+".txt"));
					out=new PrintWriter(new FileWriter(args[1]+".txt"));
				}
			ArrayStack ArSt=new ArrayStack();
			Iterator<Integer> it= ArSt.iterator();
			String input = in.readLine();
			if (input==null){
				out.println("File is empty");
				}else{
					try{
						while (input != null) {
							String[] str = input.split(" ");
							out.print("Command: "+input+" ");
							if (str[0].equals("push")){
								if(str[1].equals("null")){
									ArSt.push(null);
									}else{
										try{
											ArSt.push(Integer.parseInt(str[1]));
											}
										catch(NumberFormatException e){
											out.println("   Not correct format of data!");
										}
									}
								
								}else if (str[0].equals("peek")){
									try{
										out.println("   Result: "+ArSt.peek());
										}
									catch(EmptyStackException e){
										out.println("   Stack is empty!");
									}
									
								}else if (str[0].equalsIgnoreCase("pop")){
									try{
										out.println("   Result: "+ArSt.pop());
										}catch(EmptyStackException e){
											out.println("   Stack is empty!");
										}
									
								}else if (str[0].equals("isEmpty")){
									out.println("   Result: "+ArSt.isEmpty());
									
								}else if (str[0].equals("search")){
									if(str[1].equals("null")){
										out.println("   Result: "+ArSt.search(null));
									}else{
										try{
											out.println("   Result: "+ArSt.search(Integer.parseInt(str[1])));
										}
										catch(NumberFormatException e){
											out.println("   Not correct format of data!");
										}
									}
									
								}else if (str[0].equals("size")){
									out.println(ArSt.size());
									
								}else if (str[0].equals("clear")){
									ArSt.clear();
									
								}else if (str[0].equals("hasNext")){
									out.println("   Result: "+it.hasNext());
									
								}else if(str[0].equals("next")){
									try{
										out.println("Result: "+it.next());
									}
									catch(java.util.NoSuchElementException e){
										out.println("   Iteration has no more elements!");
									}
									
								}else{
									out.println("Unknown command '"+str[0]+"'");
									}
							out.print("Stack: " + ArSt.toString() + "\n");
							input = in.readLine();
						}
					}catch(IOException e){
						out.println(e.toString());
					}
                                
                        }
                }finally{
                        if (out!=null) out.close();
                        if(out.checkError())
                        	System.out.println("File close error!");
                        try{
                        if (in!=null) in.close();   
                        }
                        catch(IOException e){
                        	System.out.println(e.toString());
                        }
                }
            
    }
}